/**
 * Created by Administrator on 2017/5/18.
 */
function init(){
	var page = 1;
	var rows = 10;
	/**
	 * 新增方法
	 */
	$("#btn-add").click(function(e){
		 layer.open({
		      type: 1 //Page层类型
		      ,btn:["确定","取消"]
		      ,title: '新增商品品牌'
		      ,skin: 'layui-layer-prompt'
		      ,content: "<div class=''><input type='brandName' class='product_input' placeholder='品牌名称'></input><input class='product_input'  type='seq' placeholder='排序'></input></div>"
		      ,yes: function(index, layero){
		    	  var brandName=$(layero).find("input[type='brandName']").val();
		    	  var seq=$(layero).find("input[type='seq']").val()
		    	  $.ajax({
						type: "post",
						url: '/diconet/productBrand/save',
						data: {
							brandName : brandName,
							seq : seq
						},
						dataType: 'json',
						success: function(data){
							if(data.success == 'T'){
								layer.close(index);
								var brandName = $("#brandName").val();
								loadComment(page,rows,brandName);
							}else{
								layer.msg(data.message, {icon: 5,time: 1000});
							}
						},
						error: function(data){
							layer.msg('新增失败', {icon: 5,time: 1000});
						}
					});
		      }
		    });
			
		});
	/**
	 * 修改方法
	 */
	$(".brandList").on('click','.btn-xiugai',function(e){
		var id = $(e.target.parentElement).prev().val();
		layer.prompt({title: '修改商品品牌', formType: 0,value: $(e.target.parentElement).prev().prev().text()}, function(brandName, index){
			$.ajax({
				type: "post",
				url: '/diconet/productBrand/update',
				data: {
					id : id,
					brandName : brandName
				},
				dataType: 'json',
				success: function(data){
					layer.close(index);
					var brandName = $("#brandName").val();
			    	loadComment(page,rows,brandName);
				},
				error: function(data){
					layer.msg('修改失败', {icon: 5,time: 1000});
				}
			});
		});
	});
	/**
	 * 删除方法
	 */
	$(".brandList").on('click','.btn-del',function(e){
		var id = $(e.target.parentElement).prev().val();
		layer.confirm('是否要删除此条记录？', {
			btn: ['删除','取消'], //按钮
			title: '提示'
		}, function(index){
			$.ajax({
				type: "post",
				url: '/diconet/productBrand/remove',
				data: {
					id : id
				},
				dataType: 'json',
				success: function(data){
					layer.close(index);
					var brandName = $("#brandName").val();
			    	loadComment(page,rows,brandName);
				},
				error: function(data){
					layer.msg('删除失败', {icon: 5,time: 1000});
				}
			});
		});
	});
	$(".quxiao").click(function(){
	    $("#submit-block").css("display","none");
	});
	$(".btn-del").click(function(){
		var objId = $(this).parent().siblings("input").val();
	    if($("#submit-block").css("display")=="none"){
	        $("#remove-block").css("display","block").attr('data-id',objId);
	    }else{
	        return false;
	    }
	});
	$('#remove-block .confirm input.queren').click(function(){
		var id=$(this).parent().parent().attr('data-id');
		deleteBrand(id);
	});

	$(".confirm>input").click(function(){
	    $("#remove-block").css("display","none");
	});
	// 搜索框搜索
    $('.search-box').on('click','span',function(){
    	var brandName = $("#brandName").val();
    	loadComment(page,rows,brandName);
    });
	loadComment(page,rows,'');
}

/**
 * 加载列表数据
 * @param page
 * @param rows
 */
function loadComment(page, rows, brandName) {
	$.ajax({
		type: "post",
		url: '/diconet/productBrand/list/page',
		data: {
			page : page,
			rows : rows,
			brandName : brandName
		},
		dataType: 'json',
		success: function(data){
			var commentContent = '';
			console.log(data);
			if(data) {
				for(var i=0; i<data.rows.length; i++) {
					var comment = data.rows[i];
					var date1=comment.publishDate == null?'':comment.publishDate;
					commentContent += '<div class="brand-item">';
					commentContent += '<h5>'+comment.brandName+'</h5>';
					commentContent += '<input type="hidden" value="'+comment.id+'" />';// 商品图片
					commentContent += '<div class="set-btn"><button class="btn-xiugai">修改</button><button class="btn-del" >删除</button></div>';// 商品代码商品名称
					commentContent += '</div>';
				}
				$('.brandList').html(commentContent);
				var dataPage = data.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+',\''+brandName+'\');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+',\''+brandName+'\');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+brandName+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+brandName+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+brandName+'\');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+brandName+'\');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+',\''+brandName+'\');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+',\''+brandName+'\');">尾页</a>';
				}
			}
			$(".quotes").html(pageContent);
		},
		error : function(data){
			layer.msg('请求失败', {time:500,icon:5}); 
		}
	});
}

$('.tab').on('click','div',function(){
	if($(this).html()=='商品分类'){
		window.location.href = path + "/distributor/product_set";
	}else if($(this).html()=='商品品牌'){
		window.location.href = path + "/distributor/product_brand";
	}else if($(this).html()=='商品单位'){
		window.location.href = path + "/distributor/product_unit";
	}
});

init();